#! /bin/sh /usr/share/dpatch/dpatch-run
## 06_unit_dbase.dpatch by  <Mazen Neifer <mazen.neifer@supaero.org>>
##
## All lines beginning with `## DP:' are a description of the patch.
## DP: This patch fixes a non deterministic register allocation for
## DP: sprac architecture and permits long jumps for huge programs.

@DPATCH@
Index: fpcsrc/compiler/sparc/cgcpu.pas
===================================================================
--- fpc-2.2.0~/fpcsrc/compiler/sparc/cgcpu.pas
+++ fpc-2.2.0/fpcsrc/compiler/sparc/cgcpu.pas
@@ -489,7 +489,7 @@
           end
         else
           begin
-            case fromsize of
+            case tosize of
               { signed integer registers }
               OS_8,
               OS_S8:
@@ -540,6 +540,9 @@
                  InternalError(2002122101);
              end;
              handle_load_store(list,false,op,reg,ref);
+             if (fromsize=OS_S8) and
+                (tosize=OS_16) then
+               a_load_reg_reg(list,fromsize,tosize,reg,reg);
            end;
       end;
 
@@ -1342,12 +1345,19 @@
             reference_reset_base(href,NR_O0,0);
             cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,NR_L0);
             { jmp *vmtoffs(%eax) ; method offs }
-            reference_reset_base(href,NR_L0,procdef._class.vmtmethodoffset(procdef.extnumber));
+            reference_reset_base(href,NR_G1,procdef._class.vmtmethodoffset(procdef.extnumber));
             list.concat(taicpu.op_ref_reg(A_LD,href,NR_L1));
-            list.concat(taicpu.op_reg(A_JMP,NR_L1));
+            list.concat(taicpu.op_reg(A_JMP,NR_G1));
           end
         else
-          list.concat(taicpu.op_sym(A_BA,current_asmdata.RefAsmSymbol(procdef.mangledname)));
+          begin
+            reference_reset_symbol(href,current_asmdata.RefAsmSymbol(procdef.mangledname),0);
+            href.refaddr := addr_hi;
+            list.concat(taicpu.op_ref_reg(A_SETHI,href,NR_G1));
+            href.refaddr := addr_lo;
+            list.concat(taicpu.op_reg_ref_reg(A_OR,NR_G1,href,NR_G1));
+            list.concat(taicpu.op_reg(A_JMP,NR_G1));
+          end;
         { Delay slot }
         list.Concat(TAiCpu.Op_none(A_NOP));
 
